package com.aiju.cash.ui.main.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiju.bean.Shop;
import com.aiju.bean.User;
import com.aiju.cash.R;
import com.aiju.common.image.AiJuImageLoaderProxy;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：流冥 on 2017/7/19 16:22
 * 说明：
 */

public class BindStoreAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Shop> list = new ArrayList<>();
    private User mUser;

    public BindStoreAdapter(List<Shop> shopList, User user) {
        this.list = shopList;
        mUser = user;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_shop_manager, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new ShopManagerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        ShopManagerViewHolder holder = (ShopManagerViewHolder) viewHolder;
        holder.position = i;
        Shop manager = list.get(i);
        showView(((ShopManagerViewHolder) viewHolder), manager);

    }


    @Override
    public int getItemCount() {
        return list.size();
    }


    class ShopManagerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public View rootView;
        public ImageView image;
        public TextView title;
        public TextView subTitle;
        public TextView audit_state;
        public ImageView check;
        public ImageView shopType;
        public int position;

        public ShopManagerViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            check = (ImageView) itemView.findViewById(R.id.check);
            shopType = (ImageView) itemView.findViewById(R.id.shop_type);
            title = (TextView) itemView.findViewById(R.id.title);
            subTitle = (TextView) itemView.findViewById(R.id.sub_title);
            audit_state = (TextView) itemView.findViewById(R.id.audit_state);
            rootView = itemView.findViewById(R.id.parent_view);
            rootView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
//            if (null != onRecyclerViewListener) {
//                onRecyclerViewListener.onManageShop(list.get(position));
//            }

            Shop shop = list.get(position);
            if (shop.isERPGranted())
                return;
            if (shop.isSelected())
                shop.setSelected(false);
            else
                shop.setSelected(true);
            notifyDataSetChanged();

        }
    }

    private OnClickListener onRecyclerViewListener;

    public interface OnClickListener {

        void onManageShop(Shop shopManager);
    }

    public void setListener(OnClickListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    /**
     * 根据type进行适配
     */
    private void showView(ShopManagerViewHolder viewHolder, Shop shopManager) {
        showCommonView(viewHolder, shopManager);
        viewHolder.check.setVisibility(View.VISIBLE);
        if (shopManager.isERPGranted()) {
            viewHolder.check.setImageResource(R.mipmap.icon_no_sel);
        } else {
            if (shopManager.isSelected()) {
                viewHolder.check.setImageResource(R.mipmap.ic_account_type_select);
            } else {
                viewHolder.check.setImageResource(R.mipmap.ic_account_type_unselect);
            }
        }
    }

    /**
     * 显示公有view
     */
    private void showCommonView(ShopManagerViewHolder viewHolder, Shop shopManager) {
        AiJuImageLoaderProxy.getInstance().displayImage(shopManager.getImageUrl(), viewHolder.image, R.mipmap.icon_empty);
        viewHolder.title.setText(shopManager.getShopName());
        setIsReputationShopTextView(viewHolder.subTitle, shopManager.getIsReputationShop());
        showLeftTopImageByTag(shopManager.getShopType(), viewHolder.shopType);
    }


    /**
     * 显示item左上角商家店铺类型：总店、直营点、加盟店
     * 门店类型（0,总店 1，直营店，2加盟店）
     */
    private void showLeftTopImageByTag(String shopTag, ImageView imageView) {
        switch (shopTag) {
            case "0":
                imageView.setImageResource(R.mipmap.icon_left_zongdian);
                break;
            case "1":
                imageView.setImageResource(R.mipmap.icon_left_zhiying);
                break;
            case "2":
                imageView.setImageResource(R.mipmap.icon_left_jiameng);
                break;
            default:
                break;
        }
    }


    /**
     * 口碑店铺（1）--，普通店铺（0）
     */
    private void setIsReputationShopTextView(TextView textView, String isReputationShop) {
        if ("1".equals(isReputationShop)) {
            textView.setText("口碑店铺");
        } else if ("0".equals(isReputationShop)) {
            textView.setText("普通店铺");
        }

    }


    public String getStoreIds() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isSelected()||list.get(i).isERPGranted()) {
                sb.append(list.get(i).getStoreId() + ",");
            }
        }
        if (sb.length() != 0) {
            StringBuilder stringBuilder = sb.deleteCharAt(sb.length() - 1);
            return stringBuilder.toString();
        }
        return "";
    }

    public String getStoreNames() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isSelected()) {
                sb.append(list.get(i).getShopName() + ",");
            }
        }
        StringBuilder stringBuilder = sb.deleteCharAt(sb.length() - 1);
        return stringBuilder.toString();
    }
}
